package com.lingjtx.common.security.common;

import com.lingjtx.common.core.ResponseResult;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

public class Utils {
    public static final Logger log = LoggerFactory.getLogger(Utils.class);

    public static void printResponseResult(HttpServletResponse response, ResponseResult responseResult) {
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Origin", "*");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.print(responseResult);
            out.flush();
        } catch (IOException e) {
            log.error("printResponseResult Exception : {}", e.getMessage());
        } finally {
            if (Objects.nonNull(out))
                out.close();
        }
    }
}
